package com.anlu.thread.ta4;

/**
 * @description:
 * @author: Andrew
 * @create: 2019-01-30 15:58
 **/
public class Main {
    private int value;
    private Mylock2 lock=new Mylock2();

    public int next(){
        lock.lock();

        try{
            Thread.sleep(1);
            return value++;
        }catch (InterruptedException e){
            throw new RuntimeException();
        }finally {
            lock.unlock();
        }
    }

    public void a() {
        lock.lock();
        System.out.println("a");
        b();
        lock.unlock();
    }

    public void b() {
        lock.lock();
        System.out.println("b");
        lock.unlock();
    }

    public static void main(String[] args){
        Main main=new Main();
        new Thread(new Runnable() {
            @Override
            public void run() {
                main.a();
            }
        }).start();

        for (int i=0;i<1000;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    main.next();
                }
            }).start();
        }

        while (Thread.activeCount()>2){

            System.out.println(Thread.activeCount());
            Thread.yield();
        }

        System.out.println("最后的value:"+main.value);
    }
}
